package main

import (
	"context"
	"fmt"
	"github.com/coreos/etcd/clientv3"
	"github.com/coreos/etcd/pkg/transport"
	"log"
	"time"
)

var (
	dialTimeout    = 5 * time.Second
	requestTimeout = 10 * time.Second
	endpoints      = []string{"20.26.38.130:2379"}
)

func main() {
	tlsInfo := transport.TLSInfo{
		CertFile:      "ssl/kubernetes.pem",
		KeyFile:       "ssl/kubernetes-key.pem",
		TrustedCAFile: "ssl/ca.pem",
	}
	tlsConfig, err := tlsInfo.ClientConfig()
	if err != nil {
		log.Fatal(err)
	}
	cli, err := clientv3.New(clientv3.Config{
		Endpoints:   endpoints,
		DialTimeout: dialTimeout,
		TLS:         tlsConfig,
	})
	if err != nil {
		log.Fatal(err)
	}
	defer cli.Close() // make sure to close the client

	_, err = cli.Put(context.TODO(), "foo", "bar")
	if err != nil {
		log.Fatal(err)
	}

	rep, err := cli.Get(context.TODO(), "foo")
	fmt.Printf("count: %d\n", rep.Count)
	fmt.Printf("Kvs: %s\n", rep.Kvs)

	rch := cli.Watch(context.Background(), "/registry/pods", clientv3.WithPrefix())

	for wresp := range rch {
		for _, ev := range wresp.Events {
			fmt.Println(ev.IsModify())
			fmt.Println(ev.Kv.String())
		}
	}
}
